
using System;
using Gtk;
using Hildon;
using Mono.Unix;
using System.Collections;

namespace Tomboy
{

	public class ToCWindow : Gtk.Dialog
	{
		
		private Gtk.ScrolledWindow editor_window;
		private Gtk.TreeStore treeStore;
		private Gtk.TreeView tv;

		private ArrayList noteList;
		public Note note;
		
		public ToCWindow(NoteManager manager)
		{
		
			this.note=null;
			
			treeStore = new Gtk.TreeStore(typeof(string),typeof(Note));
			
			// Load Data on the treeStore
			noteList=manager.notes;
			IEnumerator en=noteList.GetEnumerator();
			while (en.MoveNext()) {
				Note note=(Note)en.Current;
				treeStore.AppendValues(note.Title,note);
			}
			
			tv = new TreeView ();
			tv.Model = treeStore;
			tv.HeadersVisible = true;
			tv.RowActivated += rowActivated;
			tv.SetSizeRequest(500,300);

			tv.AppendColumn ("Title", new CellRendererText (), "text", 0);
                        
			// editor ScrolledWindow
			editor_window = new ScrolledWindow ();
			editor_window.HscrollbarPolicy = PolicyType.Automatic;
			editor_window.VscrollbarPolicy = PolicyType.Automatic;
			editor_window.Add (tv);
			VBox.Add (editor_window);

			// Create the region where insert Push Button 			
			Button buttonOk=new Button("gtk-ok");
			buttonOk.UseStock=true;

			Button buttonCancel=new Button("gtk-cancel");
			buttonCancel.UseStock=true;
			buttonOk.Clicked += new EventHandler(buttonOkCliked);
			buttonCancel.Clicked += new EventHandler(buttonCancelCliked);
		
			AddActionWidget(buttonOk,0);
			AddActionWidget(buttonCancel,1);

		}
		
		void rowActivated (object sender, EventArgs args) {

			// Get the selected Row	    	
			TreeSelection selection=tv.Selection;
			TreeIter iter;
			TreeModel model;
			if (selection!=null) {
				TreePath[] tpSel=selection.GetSelectedRows(out model);

				if (tpSel.Length>0) {
					int pos=int.Parse(tpSel[0].ToString());
					IEnumerator en=noteList.GetEnumerator();
					int i=0;
					while (en.MoveNext() && i<pos) {
						i++;
					}
					note=(Note)en.Current;
					
//					model.GetIter(out iter,tpSel[0]);
//					note=(Note)model.GetValue(iter,1);
					
					this.Hide();
					this.Destroy();
				}
			}

		}

		public void buttonOkCliked(object o, EventArgs args) {
			rowActivated(o,args);
		}

		public void buttonCancelCliked(object o, EventArgs args) {
			this.Hide();
			this.Destroy();
		}
	}
}
